import allure

from tools.handle_replace_mark_data import replace_mark_data_by_re
from tools.global_data import GlobalData
from jsonpath import jsonpath
from loguru import logger

resp = {"access_token": "5eecb4e5-d7aa-44d1-a9ac-e2f861c96c37",
        "token_type": "bearer",
        "refresh_token": "282c1f60-2719-4858-8858-89b4e699c7db",
        "expires_in": 1295999,
        "pic": None,
        "userId": "a57f6e4a69bc456590b74084a1a61240",
        "nickName": "15091764133",
        "enabled": True}

check_resp = '[{"expr": "$..userId", "expected": False, "comp_type": "!="},' \
             '{"expr": "$..nickName", "expected": ["#new_phone#"], "comp_type": "=="}]'


def check_status_code(resp_dict):
    pass

@allure.step("===响应结果断言===")
def check_response(resp_dict, check_resp_str):
    logger.info("===断言响应结果===")
    # 替换check_resp的占位符
    check_resp_str = replace_mark_data_by_re(check_resp_str)
    # 去掉“”，转成列表格式
    check_resp_list = eval(check_resp_str)
    print(check_resp_list)

    for one_check_response_dict in check_resp_list:
        # 从响应结果中提取实际结果，要么是False，要么是list
        actual_list = jsonpath(resp_dict, one_check_response_dict["expr"])
        logger.info(f'比对类型为：{one_check_response_dict["comp_type"]}')
        logger.info(f'jsonpath提取表达式为：{one_check_response_dict["expr"]}')
        logger.info(f'提取之后，实际结果为：{actual_list}')
        logger.info(f'期望结果为：{one_check_response_dict["expected"]}')
        # 根据比对类型，拿实际结果与期望结果比对
        if one_check_response_dict.get("comp_type") == "!=":
            assert actual_list != one_check_response_dict["expected"]
        elif one_check_response_dict.get("comp_type") == "==":
            assert actual_list == one_check_response_dict["expected"]
        else:
            logger.error(f'比对类型错误，不支持{one_check_response_dict.get("comp_type")}')


if __name__ == '__main__':
    setattr(GlobalData, "new_phone", "3333333")
    check_response(resp, check_resp)
